<?php


namespace app\admin\controller\order;


use app\common\controller\Adminbase;
use EasyWeChat\Factory;
use think\Db;

class Packorder extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $filter = input('filter');
            $where[] = ['o.status', '<>', 1];
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['status'])) {
                    $where[] = ['o.status', '=', $filter['status']];
                }
                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['pay_time'])) {
                    $create_time = explode(' - ', $filter['pay_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.pay_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('package_order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->join('package p', 'p.id=o.package_id')
                ->field('o.*,p.name as package_name,p.img as package_img,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->page($page, $limit)
                ->order('o.create_time desc')
                ->order('o.pay_time desc')
                ->select();
            $total = Db::name('package_order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->join('package p', 'p.id=o.package_id')
                ->where($where)
                ->count();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                if ($_list[$k]['pay_time']) {
                    $_list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
                } else {
                    $_list[$k]['pay_time'] = "------";
                }
            }
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $package_list = Db::name('package')->where([
            ['is_del', '<>', 0],
        ])->select();
        foreach ($package_list as $k => $v) {
            $package_list[$v['id']] = $v['name'];
        }
        $this->assign([
            'package_list' => $package_list
        ]);
        return $this->fetch();
    }

    public function refund()
    {
        $order_id = $this->request->param('id/d', 0);
        Db::startTrans();
        try {
            $order_info = Db::name('package_order')->where('id', $order_id)->find();
            $config = config('wechat.wxpay');
            $app = Factory::payment($config);
            $result = $app->refund->byOutTradeNumber($order_info['order_no'], $order_info['order_no'], $order_info['pay_money'] * 100, $order_info['pay_money'] * 100, [
                // 可在此处传入其他参数，详细参数见微信支付文档
                'refund_desc' => '订单退款',
            ]);
            if ($result['result_code'] == "FAIL") {
                return ['code' => 100, 'data' => '', 'msg' => $result['err_code_des']];
            }
            tuikuanPackOrder($order_info, $order_id, 1);
            Db::commit();
            return ['code' => 200, 'data' => '', 'msg' => '操作成功'];
        } catch (\Exception $e) {
            Db::rollback();
            return ['code' => 100, 'data' => '', 'msg' => '操作失败' . $e->getMessage()];
        }
    }

    public function detail()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
        }
        $id = $this->request->param('id/d', 0);
        $order_info = Db::name('package_order')->find($id);
        $member_package_server_info = Db::name('member_package_server')
            ->where([
                ['mp_id','=',$order_info['member_package_id']],
                ['server_num','>',0],
            ])
            ->find();
        $server_info=Db::name('server')->find($member_package_server_info['server_id']);
        $this->assign([
            'order_info' => $order_info,
            'member_package_server_info'=>$member_package_server_info,
            'server_info' => $server_info
        ]);
        return $this->fetch();
    }

}